package twelfth.对象序列化;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

// public class Person implements Serializable {
public class Person implements Externalizable {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    // 覆写此方法，根据需要，可以保存属性或具体内容，序列化使用
    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeObject(this.name);//保存姓名
        out.writeInt(this.age);//保存年龄
    }

    // 覆写此方法，根据需要，读取内容，反序列化使用
    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        this.name = (String) in.readObject();// 读取姓名
        this.age = in.readInt();// 读取年龄
    }
}
